from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListAPIView

from apps.goods.models import SPU,Brand,GoodsCategory,SPUSpecification
from apps.meiduo_admin.serializers.spu import SPUSerializer,BrandSerializer,GoodsCategorySerializer,SPUSpecSerializer
from apps.meiduo_admin.utils import PageNum


#spu增删改查视图集
class SPUViewSet(ModelViewSet):
    queryset = SPU.objects.all()
    serializer_class = SPUSerializer
    pagination_class = PageNum

    #根据接口，要定义查询关键字指定查询集
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword == '' or keyword is None:
            return SPU.objects.all()
        else:
            return SPU.objects.filter(name__contains=keyword)

#获取品牌信息
class BrandView(ListAPIView):
    queryset = Brand.objects.all()
    serializer_class = BrandSerializer

#一级分类信息
class GoodsCategoryView(ListAPIView):
    # queryset = GoodsCategory.objects.filter(parent_id=None)
    serializer_class =  GoodsCategorySerializer

    def get_queryset(self):
        if self.kwargs :
            pk = self.kwargs['pk']
            return GoodsCategory.objects.filter(parent_id = pk)
        else:
            return GoodsCategory.objects.filter(parent_id=None)


class SPUSpecsView(ListAPIView):
    serializer_class = SPUSpecSerializer
    queryset = SPUSpecification.objects.all()


